﻿using System;
using System.Collections.Generic;
using System.Linq;


class FindSequenceOfEqualElements
{
    static void PrintItem(string itemFromCollection)
    {
        Console.Write(itemFromCollection);
    }

    static void FillMatrixRandomly(ref string[,] matrix)
    {
        char[] alphabetArray = { 'a', 'b', 'c', 'd', 'e', 'f', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z' };
        Random randomAlphabet = new Random();

        for (int row = 0; row < matrix.GetLength(0); row++)
        {
            for (int col = 0; col < matrix.GetLength(1); col++)
            {
                matrix[row, col] = alphabetArray[randomAlphabet.Next(0, 2)].ToString();

            }
        }
    }

    static void PrintMatrix(ref string[,] matrix)
    {
        for (int row = 0; row < matrix.GetLength(0); row++)
        {
            for (int col = 0; col < matrix.GetLength(1); col++)
            {
                Console.Write("{0}, ", matrix[row, col]);

            }
            Console.WriteLine();

        }
    }

    static List<string> SearchMatrixByRowAndColumn(ref string[,] matrix)
    {
        List<string> sequenceOfEqualElements = new List<string>();
        //sequenceOfEqualElements.TrimExcess();

        string currentElement;

        for (int col = 0; col < matrix.GetLength(1); col++)
        {
            //temp list to save the equal elements
            List<string> tempSequenceOfEqualElements = new List<string>();

            for (int row = 0; row < matrix.GetLength(0); row++)
            {
                currentElement = matrix[row, col];

                if (tempSequenceOfEqualElements.IndexOf(currentElement) > -1)
                {
                    tempSequenceOfEqualElements.Add(currentElement);
                    
                }
                else
                {
                    tempSequenceOfEqualElements.Clear();
                    tempSequenceOfEqualElements.Add(currentElement);

                }

                //tempSequenceOfEqualElements.TrimExcess();

                if (tempSequenceOfEqualElements.Count() > sequenceOfEqualElements.Count())
                {
                    sequenceOfEqualElements.Clear();
                    sequenceOfEqualElements = tempSequenceOfEqualElements.ToList();
                    //sequenceOfEqualElements.TrimExcess();

                }
            }
        }

        for (int row = 0; row < matrix.GetLength(0); row++)
        {
            //temp list to save the equal elements
            List<string> tempSequenceOfEqualElements = new List<string>();

            for (int col = 0; col < matrix.GetLength(1); col++)
            {
                currentElement = matrix[row, col];

                if (tempSequenceOfEqualElements.IndexOf(currentElement) > -1)
                {
                    tempSequenceOfEqualElements.Add(currentElement);

                }
                else
                {
                    tempSequenceOfEqualElements.Clear();
                    tempSequenceOfEqualElements.Add(currentElement);

                }

                //tempSequenceOfEqualElements.TrimExcess();

                if (tempSequenceOfEqualElements.Count() > sequenceOfEqualElements.Count())
                {
                    sequenceOfEqualElements.Clear();
                    sequenceOfEqualElements = tempSequenceOfEqualElements.ToList();
                    //sequenceOfEqualElements.TrimExcess();

                }
            }
        }

        return sequenceOfEqualElements;
    }


    static List<string> SearchByDiagonals(ref string[,] matrix)
    {
        List<string> sequenceOfEqualElements = new List<string>();
        //sequenceOfEqualElements.TrimExcess();

        string currentElement;
        List<string> tempSequenceOfEqualElements = new List<string>();

        //search under(left) the big diagonal (top left to bottom right), e.g. left part of the matrix
        //for (int row = matrix.GetLength(0) - 1; row >= 0; row--)
        //{

        //    int diagonalColumn = 0;

        //    for (int diagonalPoint = row; diagonalPoint < matrix.GetLength(0); diagonalPoint++)
        //    {
        //        if (diagonalColumn < matrix.GetLength(1) - 1)
        //        {
        //            diagonalColumn++;
        //        }
        //        else
        //        {
        //            break;
        //        }

        //        currentElement = matrix[diagonalPoint, diagonalColumn];

        //        if (tempSequenceOfEqualElements.IndexOf(currentElement) > -1)
        //        {
        //            tempSequenceOfEqualElements.Add(currentElement);

        //        }
        //        else
        //        {
        //            tempSequenceOfEqualElements.Clear();
        //            tempSequenceOfEqualElements.Add(currentElement);

        //        }

        //        //tempSequenceOfEqualElements.TrimExcess();

        //        if (tempSequenceOfEqualElements.Count() > sequenceOfEqualElements.Count())
        //        {
        //            sequenceOfEqualElements.Clear();
        //            sequenceOfEqualElements = tempSequenceOfEqualElements.ToList();
        //            //sequenceOfEqualElements.TrimExcess();

        //        }
        //    }
        //    tempSequenceOfEqualElements.Clear();

        //}

        //search above(right) the big diagonal (top left to bottom right), e.g. right part of the matrix
        for (int column = 1; column < matrix.GetLength(1); column++)
        {
            int diagonalRow = 0;
            for (int diagonalPoint = column; diagonalPoint < matrix.GetLength(1); diagonalPoint++)
            {

                if (diagonalRow<matrix.GetLength(0)-1)
                {
                    diagonalRow++;    
                }
                else
                {
                    break;
                }

                currentElement = matrix[diagonalRow, diagonalPoint];
                if (tempSequenceOfEqualElements.IndexOf(currentElement) > -1)
                {
                    tempSequenceOfEqualElements.Add(currentElement);

                }
                else
                {
                    tempSequenceOfEqualElements.Clear();
                    tempSequenceOfEqualElements.Add(currentElement);

                }

                //tempSequenceOfEqualElements.TrimExcess();

                if (tempSequenceOfEqualElements.Count() > sequenceOfEqualElements.Count())
                {
                    sequenceOfEqualElements.Clear();
                    sequenceOfEqualElements = tempSequenceOfEqualElements.ToList();
                    //sequenceOfEqualElements.TrimExcess();

                }
            }
            tempSequenceOfEqualElements.Clear();

        }
        return sequenceOfEqualElements;
    }

    static void Main()
    {
        while (true)
        {
            int matrixRows = int.Parse(Console.ReadLine());
            int matrixColumns = int.Parse(Console.ReadLine());

            string[,] matrix = new string[matrixRows, matrixColumns];

            FillMatrixRandomly(ref matrix);
            PrintMatrix(ref matrix);

            Console.WriteLine("\n\n\n\n");

            //SearchMatrixByRowAndColumn(ref matrix).ForEach(PrintItem);
            Console.WriteLine();
            SearchByDiagonals(ref matrix).ForEach(PrintItem);

            Console.WriteLine();
        }
    }
}

